﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace GZ.Common
{
    public class IdentifyHelper
    {
        private static IdentifyHelper instance;
        public IdentifyHelper()
        {

        }
        /// <summary>
        /// 取得唯一标识生成类的单例
        /// </summary>
        public static IdentifyHelper Instance
        {
            get
            {
                if (instance == null)
                    instance = new IdentifyHelper();
                return instance;
            }
        }

        /// <summary>
        /// 产生订单Id格式为1204020001
        /// </summary>
        /// <returns>订单号</returns>
        public Int64 GetOrderId()
        {
            string sql = @"SELECT count(1) FROM  tb_serialnumber WHERE InDate>= @Begin and InDate < @End";
            DateTime Begin = DateTime.Now.Date;
            DateTime End = DateTime.Now.AddDays(1).Date;
            MySqlParameter[] parameters = new MySqlParameter[2];
            parameters[0] = new MySqlParameter("@Begin", Begin);
            parameters[1] = new MySqlParameter("@End", End);
            int val = Convert.ToInt32(MySqlHelper.ExecuteScalar(GZ.DAL.MySQLHelper.connStr, sql, parameters));
            Int64 OrderId = Convert.ToInt64(Begin.Date.ToString("yyMMdd")) * 1000 + val+1;
            sql = "INSERT INTO tb_serialnumber (SerialNumber,InDate)VALUES(@SerialNumber,@InDate)";
            parameters = new MySqlParameter[2];
            parameters[0] = new MySqlParameter("@SerialNumber", OrderId);
            parameters[1] = new MySqlParameter("@InDate", Begin);
            MySqlHelper.ExecuteNonQuery(GZ.DAL.MySQLHelper.connStr, sql, parameters);
            return OrderId;
        }
    }
}
